home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Programming / BlitzBasic / dark / lightning / test.mail < prev    next >
Encoding:
Text File  |  1998-06-24  |  3.4 KB  |  121 lines

  1. From    : C.Dimitrakakis (MBGE4CD1@fs1.ee.man.ac.uk)
  2. Subject : Reducing colors of a shape, with dithering.
  3.  
  4. Ok, this is the extendend version with dithering!
  5. Rate and r2 are the rates of proportionality  of
  6. dither colour usage. (How much % will be used for each colour)
  7. Anyway, this is *not* the correct rate. It is just an estimate.
  8. For a correct rate
  9. if the dithering pair is represented by the points A,B in space
  10. and point T represents the original color,
  11. then find point C where AB and TC intersect, with TC being
  12. perpendicular to AB, thus finding the ratio AC/BC
  13.  
  14. Altough that above gives you always the correct ratio,
  15. this one is almost as good. (but not always),
  16. but is a lot,lot faster.
  17. So, take a look:
  18.  
  19. *************************
  20.  
  21. SCREEN 11
  22. sources = 4
  23. targets = 8
  24. depth = 16
  25. RANDOMIZE TIMER
  26. DIM r(sources), g(sources), b(sources)
  27. DIM red(targets), green(targets), blue(targets)
  28. DIM rd(targets), gd(targets), bd(targets)
  29.  
  30. 'Setting up Initial colours
  31.  
  32. FOR n = 1 TO sources
  33.    r(n) = INT(RND * depth)
  34.    g(n) = INT(RND * depth)
  35.    b(n) = INT(RND * depth)
  36.    PRINT r(n), g(n), b(n)
  37. NEXT n
  38.  
  39. PRINT "-------------------------------------"
  40.  
  41. 'setting target colours
  42. FOR n = 1 TO targets
  43.    red(n) = INT(RND * depth)
  44.    green(n) = INT(RND * depth)
  45.    blue(n) = INT(RND * depth)
  46. PRINT red(n), green(n), blue(n)
  47. NEXT n
  48.  
  49. 'remap colours
  50. FOR m = 1 TO sources
  51.    PRINT "Source", r(m), g(m), b(m)
  52.    dd = -1
  53.    sel = -1
  54.       FOR n = 1 TO targets
  55.          dr = red(n) - r(m)
  56.          dg = green(n) - g(m)
  57.          db = blue(n) - b(m)
  58.          d = SQR(dr ^ 2 + dg ^ 2 + db ^ 2)
  59.          'PRINT red(n), green(n), blue(n), d
  60.          IF dd <> -1 THEN 'if there has been a previous selection
  61.             IF d < dd THEN 'compare selections
  62.                dd = d 'select new color if
  63.                sel = n 'new target closer to source color
  64.             END IF
  65.          ELSE        'if there was no previous selection
  66.             dd = d   'select new color
  67.             sel = n
  68.          END IF
  69.    NEXT n
  70. 'show selected color
  71.    PRINT "Target", red(sel), green(sel), blue(sel), dd
  72.  
  73. 'create table for possible dithers
  74.  
  75.    FOR n = 1 TO targets
  76.       rd(n) = (red(sel) + red(n)) / 2
  77.       gd(n) = (green(sel) + green(n)) / 2
  78.       bd(n) = (blue(sel) + blue(n)) / 2
  79.    NEXT n
  80.  
  81. 'find best dithering pair
  82.    dd2 = -1
  83.    sel2 = -1
  84.    FOR n = 1 TO targets
  85.          dr = rd(n) - r(m)
  86.          dg = gd(n) - g(m)
  87.          db = bd(n) - b(m)
  88.          d = SQR(dr ^ 2 + dg ^ 2 + db ^ 2)
  89. '         PRINT rd(n), gd(n), bd(n), d
  90.          IF dd2 <> -1 THEN 'if there has been a previous selection
  91.             IF d < dd2 THEN 'compare selections
  92.                dd2 = d 'select new color if
  93.                sel2 = n 'new target closer to source color
  94.             END IF
  95.          ELSE        'if there was no previous selection
  96.             dd2 = d   'select new color
  97.             sel2 = n
  98.          END IF
  99.    NEXT n
  100.    PRINT "Target2", red(sel2), green(sel2), blue(sel2)
  101.    PRINT "Average", rd(sel2), gd(sel2), bd(sel2), dd2
  102. 'Now, take a look at the distances
  103. 'to find the rate of proportionality
  104.    rate = dd2 / dd
  105.    PRINT "Rate: "; rate
  106. 'and that is the result of dithering
  107.    r2 = (1 - rate)
  108. 'the dithered colour
  109.    tr = red(sel) * rate + red(sel2) * r2
  110.    tg = green(sel) * rate + green(sel2) * r2
  111.    tb = blue(sel) * rate + blue(sel2) * r2
  112.    dr = tr - r(m)
  113.    dg = tg - g(m)
  114.    db = tb - b(m)
  115. 'the distance of the dithered colour
  116.    td = SQR(dr ^ 2 + dg ^ 2 + db ^ 2)
  117.    PRINT "Dithered", tr, tg, tb, td
  118. NEXT m
  119.  
  120.  
  121.